package com.admin.utils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author Uni
 * @create 2021/12/2 23:12
 */
public class JDBCBatchUtil {
    static List<String> list_names = new ArrayList<>();
    static Random random = new Random();
    static void reset(){
        list_names.clear();
        String [] names= new String[] {"荆钰茜","冯小龙","张潇铖","李浩","王宇","张力扬","陈双龙", "陈烜泽",
                "陈子安","杜海滨","费正新","冯超凡","傅婧茹","高悦","何宾","何基恒","何中佳","胡艺峰","季冬平",
                " 金成","林介相","林津亦","刘胜军","刘哲闰","卢迦勒","马引","梅纲正","邵晨峰","石瑛捷","孙丽娜",
                "严超超","杨剑波","尹良程","应斯玮","张家瑞","张赛康","张祥","张秀秀","章涵涛","赵进涛","郑淑雅",
                "郑文星","钟萍萍", "朱李慧"};
        for(String name : names) list_names.add(name);
    }
    public static void insertRandom(){
        Connection con = JDBCUtil.getConnect();
        String sql = "insert into employee(name, age, email) values (?, ?, ?)";
        PreparedStatement pstmt =null;
        reset();
        try{
            pstmt = con.prepareStatement(sql);
            for (int i = 0; i < list_names.size(); i++) {
                String name = list_names.get(random.nextInt(list_names.size()));
                list_names.remove(name);
                pstmt.setString(1, name);
                pstmt.setInt(2, random.nextInt(4) +18);
                pstmt.setString(3, random.nextInt(1000000) + "@wzu.com");
                pstmt.addBatch();
            }
            pstmt.executeBatch();  // 批处理功能
            JDBCUtil.release(con, pstmt, null);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

}
